package cn.kgc.javaweb.servlet;

import cn.hutool.json.JSONUtil;
import cn.kgc.javaweb.dto.UserPageDTO;
import cn.kgc.javaweb.entity.House;
import cn.kgc.javaweb.service.HouseService;
import cn.kgc.javaweb.service.impl.HouseServiceImpl;
import cn.kgc.javaweb.util.JsonResultWriter;
import cn.kgc.javaweb.util.ResponseResult;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/house")
public class HouseServlet extends BaseServlet {

    private HouseService houseService = new HouseServiceImpl();

    // 用户信息分页查询  查询所有用户信息分页    搜索用户信息分页
    protected void selectPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));
        String searchParams = req.getParameter("searchParams");


        UserPageDTO userPageDTO = UserPageDTO.builder().limit(limit).page(page).searchParams(searchParams).build();
        ResponseResult responseResult = houseService.selectPage(userPageDTO);
        JsonResultWriter.writerJson(resp, responseResult);

    }


    // 根据id 查询用户信息
    protected void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer id = Integer.valueOf(req.getParameter("id"));
        ResponseResult responseResult = houseService.selectById(id);
        JsonResultWriter.writerJson(resp, responseResult);

    }


    // 修改用户信息
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // json 字符串
        String house = req.getParameter("house");
        Integer id = Integer.valueOf(req.getParameter("id"));

        House hous = JSONUtil.toBean(house, House.class);
        System.out.println("usr = " + hous);

        ResponseResult responseResult = houseService.update(hous,id);
        JsonResultWriter.writerJson(resp, responseResult);

    }

    // 添加用户信息
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String house = req.getParameter("house");
        House hous = JSONUtil.toBean(house, House.class);
        System.out.println("uni = " + hous);
        ResponseResult responseResult = houseService.add(hous);
        JsonResultWriter.writerJson(resp, responseResult);
    }

    // 删除用户信息
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String ids = req.getParameter("ids");

        ResponseResult responseResult =  houseService.delete(ids);
        JsonResultWriter.writerJson(resp, responseResult);


    }

    protected void uppdatetype(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String state = req.getParameter("stateee");
        String id = req.getParameter("id");

        ResponseResult responseResult =  houseService.uppdatetype(state,id);
        JsonResultWriter.writerJson(resp, responseResult);
    }


}
